package com.woman.beautylive.presentation.ui.main.find.pengyouquan;

import android.app.backup.FullBackupDataOutput;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.Loader;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.woman.beautylive.BeautyLiveApplication;
import com.woman.beautylive.R;
import com.woman.beautylive.domain.LocalDataManager;
import com.woman.beautylive.presentation.ui.base.BaseActivity;
import com.woman.beautylive.presentation.ui.base.ptr.BasePtr;
import com.woman.beautylive.presentation.ui.main.find.String2Spanned;
import com.woman.beautylive.presentation.ui.main.find.XListView;
import com.woman.beautylive.presentation.ui.main.find.paihangbangbean.DongTaiDetailBean;
import com.woman.beautylive.presentation.ui.main.find.paihangbangbean.DongTaiListBean;
import com.woman.beautylive.presentation.ui.main.find.paihangbangbean.MyDongTaiBean;
import com.woman.beautylive.util.Const;
import com.woman.beautylive.util.XImageLoader;

import java.util.ArrayList;
import java.util.List;

import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * Created by admin on 2016/9/22.
 */
public class WodeDongtaiActivity extends BaseActivity implements PublishInterFace, XListView.IXListViewListener {
    TextView publish;
    DongtaiPresenter dongtaiPresenter;
    DongtaiListAdapter adapter;
    XListView listView;
    List<MyDongTaiBean> arrayList = new ArrayList<>();
    PtrFrameLayout commend_anchor_ptr;
    int pageNo = 1;
    ImageView back;
    public static String REFRESHMYLIST="my_list";
    @Override
    protected int getLayoutId() {
        return R.layout.activity_wodedongtai;
    }

    @Override
    protected void findViews(Bundle savedInstanceState) {
//        adapter=new DongtaiListAdapter();


        IntentFilter intentFilter=new IntentFilter(REFRESHMYLIST);
        registerReceiver(receiver,intentFilter);
        publish = (TextView) findViewById(R.id.publish);
        listView = (XListView) findViewById(R.id.listView);
        listView.setPullLoadEnable(true);
        back= (ImageView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        listView.setPullRefreshEnable(true);
        listView.setXListViewListener(this);

        adapter = new DongtaiListAdapter(arrayList);
        listView.setAdapter(adapter);
        commend_anchor_ptr = (PtrFrameLayout) findViewById(R.id.commend_anchor_ptr);
        dongtaiPresenter = new DongtaiPresenter(this);
        pageNo = 1;
        dongtaiPresenter.getMyDongTaiList(LocalDataManager.getInstance().getLoginInfo().getToken(), pageNo + "", "10");
        publish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(WodeDongtaiActivity.this, PublishActivity.class);
                startActivity(intent);
            }
        });
//        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//
//            }
//        });


    }

    BroadcastReceiver receiver=new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            dongtaiPresenter.getMyDongTaiList(LocalDataManager.getInstance().getLoginInfo().getToken(),pageNo+"","10");
        }
    };



    @Override
    protected void init() {

    }


    @Override
    public void showDataException(String msg) {
        super.showDataException(msg);
        try {
            stopLoad();
        } catch (Exception e) {

        }

    }

    @Override
    public void showPublishResult(PublishBean data) {

    }

    @Override
    public void showUpdatePublishResult(String data) {

    }

    @Override
    public void showDeletePublishResult(String data) {

    }

    @Override
    public void showdianzanResult(String data) {

    }

    @Override
    public void showDongtaiList(DongTaiListBean list) {

    }

    @Override
    public void showMyDongTaiList(List<MyDongTaiBean> list) {
        Toast.makeText(this, list.size() + "我是条数", 0).show();
        if (pageNo == 1) {
            arrayList.clear();
        }
        arrayList.addAll(list);
        adapter.notifyDataSetChanged();
        try {
            stopLoad();
        } catch (Exception e) {

        }

    }

    @Override
    public void getDetailData(DongTaiDetailBean bean) {

    }

    @Override
    public void showHuiFuOneResult(PublishBean result) {

    }

    @Override
    public void showReplyDeleteResult(String result) {

    }

    @Override
    public void getReplyList(DongTaiListBean.DongTaiItem.ReplyList data) {

    }

    @Override
    public void getDianZanList(DongTaiListBean.DongTaiItem.ReplyList data) {

    }

    @Override
    public void onRefresh() {
        pageNo = 1;
        dongtaiPresenter.getMyDongTaiList(LocalDataManager.getInstance().getLoginInfo().getToken(), pageNo + "", "10");
    }

    @Override
    public void onLoadMore() {
        pageNo = pageNo + 1;
        dongtaiPresenter.getMyDongTaiList(LocalDataManager.getInstance().getLoginInfo().getToken(), pageNo + "", "10");
    }

    public void stopLoad() {
        listView.stopLoadMore();
        listView.stopRefresh();
    }


    void gotoDetail(String pid){
        Intent intent = new Intent(WodeDongtaiActivity.this, DongTaiDetailActivity.class);
        intent.putExtra("pid", pid);
        startActivity(intent);
    }

    View.OnClickListener detailClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            gotoDetail(v.getTag()+"");
        }
    };

    public class DongtaiListAdapter extends BaseAdapter {
        List<MyDongTaiBean> list;

        public DongtaiListAdapter(List<MyDongTaiBean> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list == null ? 0 : list.size();
        }

        @Override
        public Object getItem(int position) {
            return list == null ? 0 : list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(WodeDongtaiActivity.this).inflate(R.layout.item_mydynamic, null);
                holder = new Holder();
                holder.child= (LinearLayout) convertView.findViewById(R.id.child);
                holder.item_dynamic_day= (TextView) convertView.findViewById(R.id.item_dynamic_day);
                holder.item_dynamic_month= (TextView) convertView.findViewById(R.id.item_dynamic_month);
                convertView.setTag(holder);

            } else {
                holder = (Holder) convertView.getTag();
            }
            holder.item_dynamic_day.setText(this.list.get(position).getDay() + "日");

            holder.item_dynamic_month.setText(this.list.get(position).getMonth() + "月");
            holder.child.removeAllViews();
            for (int i = 0; i < list.get(position).getDongtai().size(); i++) {
                View childView=LayoutInflater.from(WodeDongtaiActivity.this).inflate(R.layout.child_mydongtai,null);
                TextView item_dynamic_content= (TextView) childView.findViewById(R.id.item_dynamic_content);


                childView.setTag(list.get(position).getDongtai().get(i).getPid());
                childView.setOnClickListener(detailClickListener);
                ImageView img_1 = (ImageView) childView.findViewById(R.id.img_1);
                ImageView img_2 = (ImageView) childView.findViewById(R.id.img_2);
                ImageView img_3 = (ImageView) childView.findViewById(R.id.img_3);
                ImageView img_4 = (ImageView) childView.findViewById(R.id.img_4);
                ImageView img_5 = (ImageView) childView.findViewById(R.id.img_5);
                ImageView img_6 = (ImageView) childView.findViewById(R.id.img_6);
                ImageView img_7 = (ImageView) childView.findViewById(R.id.img_7);
                ImageView img_8 = (ImageView) childView.findViewById(R.id.img_8);
                ImageView img_9 = (ImageView) childView.findViewById(R.id.img_9);
                ImageView img_10 = (ImageView) childView.findViewById(R.id.img_10);

                RelativeLayout style_1 = (RelativeLayout) childView.findViewById(R.id.style_1);//一张图
                RelativeLayout style_2 = (RelativeLayout) childView.findViewById(R.id.style_2);//2
                RelativeLayout style_3 = (RelativeLayout) childView.findViewById(R.id.style_3);//3
                RelativeLayout style_4 = (RelativeLayout) childView.findViewById(R.id.style_4);//4


                item_dynamic_content.setText(String2Spanned.parsed(this.list.get(position).getDongtai().get(i).getContent()));

                if (list.get(position).getDongtai().get(i).getPics() != null) {
                    if (list.get(position).getDongtai().get(i).getPics().length == 0) {
                        style_1.setVisibility(View.GONE);
                        style_2.setVisibility(View.GONE);
                        style_3.setVisibility(View.GONE);
                        style_4.setVisibility(View.GONE);
                    } else if (list.get(position).getDongtai().get(i).getPics().length == 1) {
                        style_1.setVisibility(View.VISIBLE);
                        style_2.setVisibility(View.GONE);
                        style_3.setVisibility(View.GONE);
                        style_4.setVisibility(View.GONE);
                        XImageLoader.load(Const.IMAG_NEW_URL + list.get(position).getDongtai().get(i).getPics()[0] + ".jpg", img_1);
                    } else if (list.get(position).getDongtai().get(i).getPics().length == 2) {
                        style_1.setVisibility(View.GONE);
                        style_2.setVisibility(View.VISIBLE);
                        style_3.setVisibility(View.GONE);
                        style_4.setVisibility(View.GONE);
                        XImageLoader.load(Const.IMAG_NEW_URL + list.get(position).getDongtai().get(i).getPics()[0] + ".jpg", img_2);
                        XImageLoader.load(Const.IMAG_NEW_URL + list.get(position).getDongtai().get(i).getPics()[1] + ".jpg", img_3);
                    } else if (list.get(position).getDongtai().get(i).getPics().length == 3) {
                        style_1.setVisibility(View.GONE);
                        style_2.setVisibility(View.GONE);
                        style_3.setVisibility(View.VISIBLE);
                        style_4.setVisibility(View.GONE);
                        XImageLoader.load(Const.IMAG_NEW_URL + list.get(position).getDongtai().get(i).getPics()[1] + ".jpg", img_4);
                        XImageLoader.load(Const.IMAG_NEW_URL + list.get(position).getDongtai().get(i).getPics()[0] + ".jpg", img_5);
                        XImageLoader.load(Const.IMAG_NEW_URL + list.get(position).getDongtai().get(i).getPics()[2] + ".jpg", img_6);
                    } else if (list.get(position).getDongtai().get(i).getPics().length >= 4) {
                        style_1.setVisibility(View.GONE);
                        style_2.setVisibility(View.GONE);
                        style_3.setVisibility(View.GONE);
                        style_4.setVisibility(View.VISIBLE);
                        XImageLoader.load(Const.IMAG_NEW_URL + list.get(position).getDongtai().get(i).getPics()[1] + ".jpg", img_7);
                        XImageLoader.load(Const.IMAG_NEW_URL + list.get(position).getDongtai().get(i).getPics()[0] + ".jpg", img_8);
                        XImageLoader.load(Const.IMAG_NEW_URL + list.get(position).getDongtai().get(i).getPics()[2] + ".jpg", img_9);
                        XImageLoader.load(Const.IMAG_NEW_URL + list.get(position).getDongtai().get(i).getPics()[2] + ".jpg", img_10);
                    }
                } else {
                    style_1.setVisibility(View.GONE);
                    style_2.setVisibility(View.GONE);
                    style_3.setVisibility(View.GONE);
                    style_4.setVisibility(View.GONE);
                }
                holder.child.addView(childView);
            }

            return convertView;
        }

        class Holder {
            TextView item_dynamic_day, item_dynamic_month, item_dynamic_content;
            ImageView img_6, img_5, img_4, img_3, img_2, img_1,img_7,img_8,img_9,img_10;
            RelativeLayout style_1, style_2, style_3, style_4;
            LinearLayout child;
        }
    }
}
